<?php
// +----------------------------------------------------------------------
// | 随手拍
// +----------------------------------------------------------------------
// | Author: Helly  QQ:15655663
// +----------------------------------------------------------------------
// | Date: 2014/01/20 10:13
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;
use Think\Controller;

class SuipaiController extends Base {

    //随手拍首页
    public function index(){

        //中间广告位
        $this->assign('midad',parent::getAdvert(4,1,0,1));

        //作品列表
        $Model=M('Suipai');
        $where['status']=array('EQ',true);

        $count = $Model->where($where)->count();
        $page = $this->Pages($count, 20);
        $pageurl='?page='.urlencode('[PAGE]');
        $page->url     = __APP__.'/suipai/'.$pageurl;

        $action = I('get.action');
        $this->assign('action',$action);
        if($action == 'welcome'){
             $order = 'S.goodnum DESC'; 
        }else{
           $order = 'S.create_time DESC'; 
        }

        $Model=new Model();
        $list=$Model
            ->table(array(
                    C('DB_PREFIX').'suipai'=>'S',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('S.userid=U.userid and S.status=true')
            ->field('S.title,S.content,S.id,S.create_time,S.image,U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz')
            ->order($order)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

        $this->assign("list", $list);

        $this->assign("Page", $page->show());

        $this->display();
    }



    public function splist(){

        //作品列表
        $Model=M('Suipai');
        $where['status']=array('EQ',true);

        $count = $Model->where($where)->count();
        $page = $this->Pages($count, 12);
        $pageurl='?page='.urlencode('[PAGE]');
        $page->url     = __APP__.'/suipai/'.$pageurl;

        $list['total']=12;

        $action = I('get.action');
        $this->assign('action',$action);
        if($action == 'welcome'){
             $order = 'S.goodnum DESC'; 
        }else{
           $order = 'S.create_time DESC'; 
        }

        $Model=new Model();
        $list['result']=$Model
            ->table(array(
                    C('DB_PREFIX').'suipai'=>'S',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('S.userid=U.userid and S.status=true')
            ->field('S.title,S.content,S.id,S.create_time,S.image,S.msgnum,U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz')
            ->order($order)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        foreach($list['result'] as $key=>$value){
            $list['result'][$key]['create_time']=format_time($value['create_time']);
            $list['result'][$key]['uavatar']=getUavatar($value['uavatar']);
            $list['result'][$key]['content']=mb_substr($value['content'],0,40,'utf-8');
        }
        exit(json_encode($list));
    }


    /**
     * 随拍作品详情
     */
    public function detail(){
        $workid=I('get.workid');
        if(empty($workid)){
            $this->error('您访问的页面不存在');
        }
        //增加点击数
        $model=M('Suipai');
        $model->where(array('id'=>$workid))->setInc('clicknum');
        $detail=$model->find($workid);
        $unionModel=new Model();

        $detail=$unionModel->table(array(
                C('DB_PREFIX').'user'=>'U',
                C('DB_PREFIX').'suipai'=>'S',
            ))
            ->where('U.userid=S.userid and S.id='.$workid)
            ->field('U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz,S.id,S.title,S.image,S.all_pic,S.content,S.clicknum,S.msgnum')
            ->find();

        if(!$detail){
            $this->error('您访问的作品不存在');
        }
        
        //start yf
        $yf_thumb = array();
        $yf_pic = array();
        if(!empty($detail['all_pic']))
        {
        	$yf_arr = json_decode($detail['all_pic'], true);
        	$yf_temp = array();
        	foreach($yf_arr as $k => $v)
        	{
        		$yf_thumb[] = $v;
        		$yf_ex = explode("/", $v);
        		$yf_ex[count($yf_ex)-1] = str_replace("yf_", "", $yf_ex[count($yf_ex)-1]);
        		$yf_pic[] = implode("/", $yf_ex);
        	}
        }
        else
        {
        	$yf_arr = array();
        	$yf_thumb[] = $yf_pic[] = $detail['image'];
        }
        $detail['pic_num'] = count($yf_thumb);
        $detail['thumb'] = implode(",", $yf_thumb);
        $detail['pic'] = implode(",", $yf_pic);
        //end
		
        $this->assign('data',$detail);

        //关注数
        $this->fansnum=getCount('UserFans',array('userid'=>$detail['userid'],'status'=>true));

        //右侧广告
        $this->rightad=parent::getAdvert(4,3,0,1);

        //点赞
        $result = M('Dig')->where(array('userid'=>$this->userid,'infoid'=>$workid,'type'=>2))->find();
        $this->assign('result',$result);

        $digCount = M('Dig')->where(array('infoid'=>$workid,'type'=>2))->count();
        $this->assign('digCount',$digCount);

        //点赞人员列表
        $diguser=new Model();
        $diguerlist=$diguser->table(array(C('DB_PREFIX').'dig'=>'D',C('DB_PREFIX').'user'=>'U',))
            ->where('D.userid=U.userid and D.type=2 and D.digtype=1 and D.infoid='.$workid.'')
            ->field('U.uavatar,U.openuid')
            ->limit(10)
            ->select();
        $this->assign('diglist',$diguerlist);

        //评论
        $where['infoid']=$workid;
        $where['type']=4;
        $where['parent_id'] = 0;//yf 0915
        $count = M('UserMessage')->where($where)->count();

       // $limitRows = 5; // 设置每页记录数
        $limitRows = C('YF_PAGE');
        $page = $this->ajaxpage($count,$limitRows,'#msg','ajax_div','ajax_page_div');

        $message=$unionModel
            ->table(array(
                    C('DB_PREFIX').'user_message'=>'M',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('M.userid=U.userid and M.status=true and M.infoid='.$workid.' and M.type=4 and M.parent_id=0')
            ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid')
            ->order('M.create_time DESC')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

//         foreach($message as $k=>$v){
//             $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
//         }
            foreach($message as $k=>$v)
            {
            	$child_temp = array();
            	$child_temp = $unionModel->table(C('DB_PREFIX').'user_message')->field('*')->where(array('parent_id' => $v['id']))->order('create_time ASC')->select();
            
            	$message[$k]['childlist'] = array();
            	if($child_temp)
            	{
            		foreach($child_temp as $yk => $yv)
            		{
            			if($yv['userid'] == 0)
            			{
            				$child_temp[$yk]['nickname'] = "烘焙圈";
            				$child_temp[$yk]['openuid'] = 0;
            			}
            			else
            			{
            				$yf_user = array();
            				$yf_user = M('User')->field('openuid, nickname, uprovince, ucity, uavatar, ifrz')->where(array('userid'=>$yv['userid']))->find();
            				$child_temp[$yk]['nickname'] = $yf_user['nickname'];
            				$child_temp[$yk]['openuid'] = $yf_user['openuid'];
            				$child_temp[$yk]['uprovince'] = $yf_user['uprovince'];
            				$child_temp[$yk]['ucity'] = $yf_user['ucity'];
            				$child_temp[$yk]['uavatar'] = $yf_user['uavatar'];
            				$child_temp[$yk]['ifrz'] = $yf_user['ifrz'];
            			}
            			if($yv['touserid'] != 0)
            			{
            				$child_temp[$yk]['touser'] = M('User')->field('openuid,nickname')->where(array('userid'=>$yv['touserid']))->find();
            			}
            			else
            			{
            				$child_temp[$yk]['touser']['nickname'] = "烘焙圈";
            				$child_temp[$yk]['touser']['openuid'] = 0;
            			}
            		}
            		$message[$k]['childlist'] = $child_temp;
            	}
            	$message[$k]['tempname'] = hy_substr($v['nickname'], 4, true);
            	//$message[$k]['tempname'] = $v['nickname'];
            	$message[$k]['touser'] = array();
            	if(!empty($v['touserid']))
            	{
            		$message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
            	}
            }
            unset($where['parent_id']);
            $this->msgcount=M('UserMessage')->where($where)->count();
        $this->msgtype=4;
        $this->assign('msglist',$message);
        $this->assign('page',$page->show());

        if(IS_AJAX){
            exit($this->fetch('msglist'));//list为list.html模版
        }

        //获取会员烘焙作品数
        $this->hbworksnum=getCount('Works',array('status'=>true,'type'=>1,'userid'=>$detail['userid']));
        //获取会员随拍作品数
        $this->spworksnum=getCount('Suipai',array('status'=>true,'userid'=>$detail['userid']));
        //是否关注
        $this->isfans=getCount('UserFans',array('myid'=>$this->userid,'userid'=>$detail['userid'],'status'=>true));

        //更多随拍作品
        $map['status']=array('EQ',true);
        $map['userid']=array('EQ',$detail['userid']);
        $map['id']=array('NEQ',$workid);
        $this->moresp=M('Suipai')->field('image,id')->where($map)->limit(6)->select();

        $this->display();
    }

    /*点赞*/
    public function praise(){
        $this->check();
        $infoid = I('get.infoid');
        $userid = $this->userid;
        $spuserid = M('Suipai')->where(array('id'=>$infoid))->getField('userid');
        if($spuserid == $this->userid){
            $this->error('您不能评价自己的作品!');
        }
        else{
            $model=M('Dig');
            $data['type'] = 2;
            $data['infoid'] = $infoid;
            $data['userid'] = $userid;
            $data['digtype'] = 1;
            $data['create_time'] =time();
            $data['status'] = 1;
            $result = $model->add($data);
            if($result){
                M('Suipai')->where(array('id'=>$infoid))->setInc('goodnum',1);
                $this->success('点赞成功！','');
            }else{
                $this->success('点赞失败,请稍候再试！','');
            }
            
        }

        
    }
}